(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.localeCodes.localeCodesEditController', localeCodesEditController);
	localeCodesEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'ajaxServiceFactory', 'validatorConfig',
			'authService' ];
	function localeCodesEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, ajaxServiceFactory, validatorConfig, authService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("localeLookup");
		var  ajaxService = ajaxServiceFactory.buildAjaxService({
				getLocaleCodes:{
					url : 'do/cda/dataList/siteLocaleCode',
					method : 'post'	
				}
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.localeCodes) !== false) {
				
				$uibModalInstance.close();
			}
			
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.localeCodes = ctrl.localeCodes || [];
			ctrl.localeCodesOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.table,localeCodeselectionChanged);
			ctrl.localeCodesOptions.enableOperation = true;
			ctrl.localeCodesOptions.enableOperationRemoveSelected = true;
			
			ctrl.localeCodesOptions.data = 'vm.localeCodes';
			ctrl.localeCodesOptions.addRow = function(row){
				console.log('addRow:');
				console.log(row);
			};
			ctrl.localeCodesOptions.removeSelectedRows = function(){
				
			};
			
			function localeCodeselectionChanged(eventRow, selectedRows) {
				ctrl.selectedLocaleSeleRows = selectedRows;
			}
			
			getLocaleCodes().then(function(){
				if(ctrl.dialogsModule.params.length>0){
					ctrl.localeCodes=ctrl.dialogsModule.params;
				};
			});
		};
		
		function getLocaleCodes(){
			var defer = $q.defer();
			var locCodeOptions = [];
			ajaxService.getLocaleCodes().then(function(response) {
				if(response.data){
					angular.forEach(response.data, function(data, index, array){
						locCodeOptions.push({"value" : data.value, "label" : data.label});
						defer.resolve({});
					});
					
					ctrl.localeCodesData = locCodeOptions;
				 }
				}, angular.noop);
			return defer.promise;
		}
		
		ctrl.setLocaleCode =function(){
			setLocaleCodes(ctrl);
		};
		
		function setLocaleCodes(ctrl){
		    if(ctrl.dataWay=="B"){
		    	ctrl.dataWay="F";
		    }else{
		    	var addlc = ctrl.localeCodes;
				var all = ctrl.loCodeData;
				if(addlc.length>0){
					if(addlc.length>=2){
						for(var i=0; i<addlc.length-1; i++){
							if(addlc[i].localeCode !=null && addlc[i].localeCode.trim() !=""){
								
								if(addlc[i].localeCode == addlc[addlc.length-1].localeCode){
									addlc[addlc.length-1].localeCode="";
										modalService.openErrorDialogs({
											messages : [ 'js.localecodes.error.value-exists' ]
										});
										return;
								}
							}
						}
					}
				}
		    }
		};

		ctrl.reprocess = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			authService.reprocess(ctrl.selectedRows[0]).then(function(response) {
				var routerInfo = response.data || {}, locale = routerInfo.processState;
				delete routerInfo.processState;
				$scope.go(locale, routerInfo);
			}, angular.noop);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			angular.forEach(ctrl.dialogsModule.params, function(value, key) {
				ctrl.searchCriteria[key] = value;
			});
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);